struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};

class Solution
{
public:
    int kthLargest(TreeNode *root, int k)
    {
        int ans;
        dfs(root, k, ans);
        return ans;
    }

    void dfs(TreeNode *root, int &k, int &ans)
    {
        if (root == nullptr || k < 0)
        {
            return;
        }
        dfs(root->right, k, ans);
        if (--k == 0)
        {
            ans = root->val;
            return;
        }
        dfs(root->left, k, ans);
    }
};